//cookie 封装
var cookie = {
    //插入cookie
    set(key,value,expires){
        //获取保存天数，然后对时间进行更改
        let date = new Date();
        let da = date.setDate(date.getDate() + expires)
        //主要是锁定key属性名，expires存放时间
        document.cookie = `${key}=${value};expires=${da}`;
    },
    //读取cookie
    get(key){
        let cookies = document.cookie.split("; ");
        let JS = {}
        cookies.forEach(entm =>{
            JS[entm.split("=")[0]] = entm.split("=")[1]
        })
        return key ? JS[key] : JS;
    },
    //删除cookie
    remove(key){
        //边界值判定，有key则返回true
        if(cookie.get(key)){
            document.cookie = `${key}=dawei;expires=${new Date('2000-11-01 01:00:00')}`;
            return true;
        }else{
            return false;
        }
    }
}
//切换二维码和账号密码
let dengluFangshi = {
    add(){
        $("#jiemian-con .erweima .er1").toggle()
        $("#jiemian-con .erweima .er2").toggle()
        $("#jiemian-con .denglu .denglu2").toggle()
        $("#jiemian-con .denglu .denglu3").toggle()
    },
    qiehuan(){
        $("#jiemian-con .erweima").click(function(){
            dengluFangshi.add()
        })
        $("#jiemian-con .denglu .denglu3 .zhang").click(function(){
            dengluFangshi.add();
        })
    },
}
dengluFangshi.qiehuan();

//账号密码对应登录按钮
function input(){
    if($(".zhanghao").val() && $(".mima").val()){
        $(".anniu").attr("style","background: #f54a35;color: #fff");
    }else{
        $(".anniu").attr("style","");
    }
}
window.onload = function(){
    input()
} 
$(".zhanghao")[0].oninput = function(){
    input()
}
$(".mima")[0].oninput = function(){
    input()
}

//账号密码 登录 cookie30天
if(cookie.get("name")){
    $(".zhanghao").val(cookie.get("name"));
    $(".mima").val(cookie.get("mima"));
    $("#jizhu")[0].checked = cookie.get("zhuangtai");
}
$(".anniu").click(function(){
    let zhang = $(".zhanghao").val();
    let mima = $(".mima").val();
    if(!zhang || !mima){
        alert("请输入账号或密码");
        return;
    }
    let http = new XMLHttpRequest();
    http.open("get",`http://127.0.0.1:8088/a?urename=${zhang}&password=${mima}`);
    http.send();
    http.onreadystatechange = function(){
        if(http.readyState === 4){
            if(http.responseText === "cuo"){
                alert("账号或密码输入错误");
            }
            if(http.responseText === "dui"){
                //cookie存储
                if($("#jizhu")[0].checked){
                    if(cookie.get("name") != zhang){
                        cookie.set("name",zhang,30)
                        cookie.set("mima",mima,30)
                        cookie.set("zhuangtai",$("#jizhu")[0].checked,30)
                    }
                }
                //coolie删除
                if(!$("#jizhu")[0].checked){
                    cookie.remove("name");
                    cookie.remove("mima");
                    cookie.remove("zhuangtai");
                }
                sessionStorage.setItem("name",zhang);
                location.href = "http://127.0.0.1/zkhl/qian/index.html"
            }
        }
    }
})

window.onkeydown = function(event){
    if(event.keyCode === 13){
        $(".anniu")[0].click()
    }
}